package jh.jianguan.springboot.dto.common.security;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @description: security的用户详情类
 * @author: guoc
 * @createDate: 2020/11/30 14:37
 */
@Data
public class SecurityUserDetails implements UserDetails {

    private Integer id;
    private String username;
    private String password;
    private Boolean enabled;
    private Boolean locked;
    private List<SecurityRole> roles;

    public SecurityUserDetails(Integer id, String username, String password, Boolean enabled, Boolean locked, List<SecurityRole> roles) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.locked = locked;
        this.roles = roles;
    }

    /**
     * 获取当前用户对象所具有的角色信息
     * @return java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>
     * @author guoc
     * @date 2020/12/2 11:42
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (SecurityRole role:roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() { ////当前账户是否未过期
        return true;
    }

    @Override
    public boolean isAccountNonLocked() { //当前账户是否未锁定
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {  //当前账户密码是否未过期
        return true;
    }

    @Override
    public boolean isEnabled() { //当前账户是否可用
        return enabled;
    }
}
